package io.finer.erp.jeecg.bas.enhance;

import java.util.Map;

import org.jeecg.modules.online.cgform.enhance.CgformEnhanceJavaInter;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import io.finer.erp.jeecg.bas.entity.CreateCuttingParams;
import io.finer.erp.jeecg.bas.entity.ZrkOrderProcess;
import io.finer.erp.jeecg.bas.entity.ZzDeviceType;
import io.finer.erp.jeecg.bas.entity.ZzShoporder;
import io.finer.erp.jeecg.bas.service.IZrkCuttingService;
import io.finer.erp.jeecg.bas.service.IZrkOrderProcessService;
import io.finer.erp.jeecg.bas.service.IZzDeviceTypeService;
import io.finer.erp.jeecg.bas.service.IZzShoporderService;
/**
 * @ClassName OrderProcessEditEnhance
 * @Description 生产订单工序修改时添加的方法，判断如果是分切工序，则计算分切方案。
 * @author duany
 * @Date 2020-11-11 17:02:36
 * @version 1.0.0
 */
@Component("orderProcessEditEnhance")
public class OrderProcessEditEnhance implements CgformEnhanceJavaInter {
	@Autowired
	private IZzDeviceTypeService deviceTypeService;
	@Autowired
	private IZrkCuttingService zrkCuttingService;
	@Autowired
	private IZzShoporderService orderService;
	@Autowired
	private IZrkOrderProcessService processService;

	@Override
	public int execute(String arg0, Map<String, Object> arg1) throws BusinessException {
		if(arg1 != null) {
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(arg1));
			return this.execute(arg0, jsonObject);
		}
		return 0;
	}

	@Override
	public int execute(String arg0, JSONObject arg1) throws BusinessException {
		String processId = arg1.getString("process_id");
		ZzDeviceType zzDeviceType = deviceTypeService.getById(processId);
		String processName = zzDeviceType.getDeviceTpyeName();
		String id = arg1.getString("id");
		this.zrkCuttingService.deleteByMainId(id);
		ZrkOrderProcess orderProcess = processService.getById(id);
		String orderId = orderProcess.getOrderId();
		if(processName.contains("分切")) {
			//智能方案生成
			String categoryId = arg1.getString("fq_category");
			String fqParam = arg1.getString("fq_params");
			Integer length = arg1.getInteger("fq_length");
			ZzShoporder zzShoporder = orderService.getById(orderId);
			this.zrkCuttingService.createCuttingMethod(new CreateCuttingParams(categoryId, fqParam, length, zzShoporder.getPaperSupplier(), zzShoporder.getGlueSupplier(), zzShoporder.getFacialSupplier()), id);
		}
		return 0;
	}

}
